<?php
/**
 * *******************************************************************************
 * Class ImportedFiles - Import file to Document management
 * *******************************************************************************
 * @author $Author: damien.viguier@gmail.com $
 * @version $Rev: 27 $
 * @internal $Id: ImportedFiles.class.php 27 2013-08-18 14:15:43Z damien.viguier@gmail.com $
 * @internal $LastChangedDate: 2013-08-18 14:15:43 +0000 (Sun, 18 Aug 2013) $
 * @link $HeadURL: http://personal-ged.googlecode.com/svn/trunk/webroot/ajax/data/class/ImportedFiles.class.php $
 * *******************************************************************************
 */
include_once '../../../lib/DBManagement.class.php';

class ImportedFiles {
	/**
	 * Return Imported files total count
	 * 
	 * @static 
	 * @return Integer
	 */
	static function getTotalImportedFiles()
	{
		$l_s_sql = "SELECT COUNT(*) AS IMPFILES_NB FROM VW_FILES_TO_IMPORT;";
		$l_as_results = DBManagement::query($l_s_sql);	
		return $l_as_results[0];	
	}
	
	/**
	 * Return next imported file data from an ascendant ordered list of id
	 * 
	 * @static
	 * @param Long $p_s_previousID
	 * @return array(mixed)
	 */
	static function getNextImportedFile($p_s_previousID)
	{
		/*
			SELECT
			`IMP_FILES`.`FILE_ID`,
			`IMP_FILES`.`FILE_FILEPATH`,
			`IMP_FILES`.`FILE_EXTENSION`,
			`IMP_FILES`.`FILE_FILESIZE`,
			`IMP_FILES`.`FILE_FILENAME`,
			`IMP_FILES`.`FILE_MIME`,
			`IMP_FILES`.`IMP_CALC_DATE_YEAR`,
			`IMP_FILES`.`IMP_CALC_DATE_MONTH`,
			`IMP_FILES`.`IMP_CALC_DATE_DAY`
			FROM `pged`.`IMP_FILES`;
		*/
		
		if(is_null($p_s_previousID))
			$p_s_previousID = 0;
		
		$l_s_sql = "SELECT 
						FILE_ID,
						FILE_FILEPATH,
						FILE_EXTENSION,
						FILE_FILESIZE,
						FILE_FILENAME,
						FILE_MIME,
						IMP_CALC_DATE_YEAR,
						IMP_CALC_DATE_MONTH,
						IMP_CALC_DATE_DAY
				 	FROM VW_FILES_TO_IMPORT WHERE FILE_ID > ".$p_s_previousID." ORDER BY FILE_ID ASC;";
		$l_as_results = DBManagement::query($l_s_sql);
		return $l_as_results[0];
	}
	
	/**
	 * Return imported file data from his ID
	 *
	 * @static
	 * @param Long $p_s_previousID
	 * @return array(mixed)
	 */
	
	static function getImportedFileByID($p_s_IDToLoad)
	{
		/*
		 SELECT
		`IMP_FILES`.`FILE_ID`,
		`IMP_FILES`.`FILE_FILEPATH`,
		`IMP_FILES`.`FILE_EXTENSION`,
		`IMP_FILES`.`FILE_FILESIZE`,
		`IMP_FILES`.`FILE_FILENAME`,
		`IMP_FILES`.`FILE_MIME`,
		`IMP_FILES`.`IMP_CALC_DATE_YEAR`,
		`IMP_FILES`.`IMP_CALC_DATE_MONTH`,
		`IMP_FILES`.`IMP_CALC_DATE_DAY`
		FROM `pged`.`IMP_FILES`;
		*/
	
		if(is_null($p_s_IDToLoad))
			$p_s_IDToLoad = 0;
	
		$l_s_sql = "SELECT
						FILE_ID,
						FILE_FILEPATH,
						FILE_EXTENSION,
						FILE_FILESIZE,
						FILE_FILENAME,
						FILE_MIME,
						IMP_CALC_DATE_YEAR,
						IMP_CALC_DATE_MONTH,
						IMP_CALC_DATE_DAY
				 	FROM VW_FILES_TO_IMPORT WHERE FILE_ID = ".$p_s_IDToLoad." ORDER BY FILE_ID ASC;";
		$l_as_results = DBManagement::query($l_s_sql);
		return $l_as_results[0];
	}
	
	/**
	 * Return previous imported file data from an ascendant ordered list of id
	 *
	 * @static
	 * @param Long $p_s_previousID
	 * @throws Exception
	 * @return array(mixed)
	 */	
	static function getPreviousImportedFile($p_s_nextID)
	{
		/*
			 SELECT
				`IMP_FILES`.`FILE_ID`,
				`IMP_FILES`.`FILE_FILEPATH`,
				`IMP_FILES`.`FILE_EXTENSION`,
				`IMP_FILES`.`FILE_FILESIZE`,
				`IMP_FILES`.`FILE_FILENAME`,
				`IMP_FILES`.`FILE_MIME`,
				`IMP_FILES`.`IMP_CALC_DATE_YEAR`,
				`IMP_FILES`.`IMP_CALC_DATE_MONTH`,
				`IMP_FILES`.`IMP_CALC_DATE_DAY`
			FROM `pged`.`IMP_FILES`;
		*/
	
		if(!is_null($p_s_nextID))
		{
			$l_s_sql = "SELECT
							FILE_ID,
							FILE_FILEPATH,
							FILE_EXTENSION,
							FILE_FILESIZE,
							FILE_FILENAME,
							FILE_MIME,
							IMP_CALC_DATE_YEAR,
							IMP_CALC_DATE_MONTH,
							IMP_CALC_DATE_DAY
				 		FROM VW_FILES_TO_IMPORT WHERE FILE_ID < ".$p_s_nextID." ORDER BY FILE_ID DESC;";
			$l_as_results = DBManagement::query($l_s_sql);
			return $l_as_results[0];
		}
		else
			throw new Exception("Impossible to load from invalid ID : ".$p_s_nextID);
	}
	
	/**
	 * Import File to document
	 * 
	 * @static
	 * @param intger 	$p_s_idImpFile
	 * @param string 	$p_s_doctypeCode
	 * @param date 		$p_dte_datefile
	 */
	static function importFileToDocument($p_s_idImpFile,$p_s_doctypeCode,$p_dte_datefile)
	{
		$l_s_sql ="SELECT importFile(".$p_s_idImpFile.",'".$p_s_doctypeCode."',NOW()) AS ID_DOC;";
		$l_as_results = DBManagement::query($l_s_sql);
		return $l_as_results[0]['ID_DOC'];
	}
	
	/**
	 * Adding a simple attribute value on a document
	 * 
	 * @static
	 * @param integer 	$p_i_idDoc			Document ID
	 * @param string 	$p_s_doctypeCode	Document Type code
	 * @param integer 	$p_i_attrDefID		Attribute definition ID
	 * @param string 	$p_s_value			Attribute value to save
	 */
	static function addSimpleAttrValueToDocument($p_i_idDoc,$p_s_doctypeCode,$p_i_attrDefID,$p_s_value)
	{
		//`addSimpleAttrValueToDocument`(P_I_IDDOC  INTEGER, P_S_DOCTYPE VARCHAR(5), P_I_ATTRDEFID INT, P_LB_VAL LONGBLOB) RETURNS int(11)
		$l_s_sql ="SELECT addSimpleAttrValueToDocument(".$p_i_idDoc.",'".$p_s_doctypeCode."',".$p_i_attrDefID.",'".$p_s_value."') AS ID_ATTRDOC;";
		$l_as_results = DBManagement::query($l_s_sql);
		//print_r($l_as_results);
		return $l_as_results[0]['ID_ATTRDOC'];
	}
	
	/**
	 * Adding a list valued attribute value on a document
	 * 
	 * @static
	 * @param integer 	$p_i_idDoc
	 * @param string 	$p_s_listCode
	 * @param integer 	$p_i_attrDefID
	 * @param integer 	$p_i_attrValID
	 * @param string	$p_s_value
	 */
	static function addListedAttrValueToDocument($p_i_idDoc,$p_s_listCode,$p_i_attrDefID,$p_i_attrValID,$p_s_value)
	{
		$l_s_sql ="SELECT addListedAttrValueToDocument(".$p_i_idDoc.",'".$p_s_listCode."',".$p_i_attrDefID.",".$p_i_attrValID.",'".$p_s_value."') AS ID_ATTRDOC;";
		$l_as_results = DBManagement::query($l_s_sql);
		//print_r($l_as_results);
		return $l_as_results[0]['ID_ATTRDOC'];
	}
	
	/**
	 * Exclude a file by his ID from "Files to Import" pool
	 * 
	 * @param integer 	$p_i_idImportFile	File ID
	 * @return boolean
	 */
	static function excludeFileFromImportPool($p_i_idImportFile)
	{
		$l_s_sql ="CALL excludeFileToImportByID(".$p_i_idImportFile.");";
		DBManagement::exec($l_s_sql);
		return true;
	}
}

?>